//
//  LHRecommedCityCollectionView.m
//  Knight_Tour
//
//  Created by 庞魁 on 15-5-14.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHRecommedCityCollectionView.h"
#import "LHRecommedCityCollectionViewCell.h"
#import "LHNetworkHandle.h"
#import "LHRecommendCityModel.h"


@interface LHRecommedCityCollectionView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) UICollectionView *CollectionView;
@property (nonatomic, retain) NSMutableArray *cityArr;
@property (nonatomic, retain) LHRecommendCityModel *recommendCity;


@end

@implementation LHRecommedCityCollectionView


-(void)dealloc
{
    [_flowLayout release];
    [_CollectionView release];
    [_recommendCity release];
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        
        
        [self getData];
    }
    
    return self;
}


/**
 *  创建collectionView
 */
- (void)createRecommedCityCollectionView
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    _flowLayout.itemSize = CGSizeMake((self.frame.size.width - 30) / 2, (self.frame.size.width - 30) / 2);
    
    
    _flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 5, 10);
    
    // 创建collectionView
    self.CollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height + 14) collectionViewLayout:_flowLayout];
    [_flowLayout release];
    
    _CollectionView.backgroundColor = [UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
    
    _CollectionView.delegate = self;
    _CollectionView.dataSource = self;
    
    [self addSubview:_CollectionView];
    
    [_CollectionView release];
    
    [_CollectionView registerClass:[LHRecommedCityCollectionViewCell class] forCellWithReuseIdentifier:@"bb"];
    
}

#pragma mark - collectionViewx协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _cityArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LHRecommedCityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bb" forIndexPath:indexPath];
    

    
    // 属性传值
    cell.recommendCityModel = _cityArr[indexPath.item];
    
    
    
    return cell;
    
    
}

// 协议传值(让代理人调用协议方法)
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
        [self.delegate jumpToNextPageWithIndexPath:indexPath cityArr:_cityArr];
   
}



// 获取网络数据

- (void)getData
{
    [LHNetworkHandle getDataWithURL:@"http://api.breadtrip.com/destination/index_places/8" completion:^(id result) {
        
        self.cityArr = [NSMutableArray array];
        
        NSArray *arr = [result objectForKey:@"data"];
        
        for (NSDictionary *tempDic in arr) {
            
            self.recommendCity = [[LHRecommendCityModel alloc] initWithDic:tempDic];
            
            [_cityArr addObject:_recommendCity];
            
            [_recommendCity release];

            
        }
        
        [self createRecommedCityCollectionView];
        

        
    }];
    
}













/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
